'use strict';

const Controller = require('egg').Controller;

class SysAreaController extends Controller {
  /**
  * 添加单位
  */
  async addArea() {
    const { ctx, service } = this;
    const user_id = ctx.curAdminUser && ctx.curAdminUser.id || '';
    if (!user_id) {
      ctx.response.fail(403);
      return;
    }
    const areaparams = ctx.request.body;
    const areaModel = {};
    if (areaparams.name) {
      areaModel.name = areaparams.name;
    }
    if (areaparams.longitude) {
       areaModel.longitude = areaparams.longitude;
    }
    if (areaparams.latitude) {
       areaModel.latitude = areaparams.latitude;
    }
    if (areaparams.code) {
      areaModel.code = areaparams.code;
    }
    if (areaparams.parent_code) {
      areaModel.parent_code = areaparams.parent_code;
    }
    if (areaparams.remarks) {
      areaModel.remarks = areaparams.remarks;
    }
    const codeInfo = await service.admin.sysArea.findAreaByCode(areaparams.code);
    if (codeInfo.length > 0) {
     ctx.response.failByCode('1001', '', '该区域代码已存在');
     return;
    }
    areaModel.type = areaparams.type || 2;
    areaModel.create_time = ctx.getDate_YYYYMMDDHHmmss();
    areaModel.update_time = ctx.getDate_YYYYMMDDHHmmss();
    areaModel.creator_id = user_id;
    await service.admin.sysArea.addArea(areaModel);
    ctx.response.success();
      // 添加操作日志
    const des = `【${ctx.curAdminUser.realName}】在【${ctx.getDate_YYYY_MM_DD_HH_mm_ss()}】添加了区域【${JSON.stringify(areaModel)}】`;
    ctx.writeSysOperationLogs('添加区域', ctx.getSystemLogType('CREATE'), { curAdminUser: ctx.curAdminUser, des });

  }
  /**
  * 查询所有区域
  */
  async findAllArea() {
    const { ctx, service } = this;
    const request = ctx.request.body;
    const user_id = ctx.curAdminUser && ctx.curAdminUser.id || '';
    if (!user_id) {
        ctx.response.fail(403);
        return;
    }

    ctx.logger.debug('查询所有区域请求参数：', request);

    const result = await service.admin.sysArea.querySysArea(request);
    ctx.response.success(result);
    // 添加操作日志
    const des = `【${ctx.curAdminUser.realName}】在【${ctx.getDate_YYYY_MM_DD_HH_mm_ss()}】查询了区域列表`;
    ctx.writeSysOperationLogs('查询所有区域', ctx.getSystemLogType('QUERY'), { curAdminUser: ctx.curAdminUser, des });
  }
  /**
  * 删除区域
  */
  async delArea() {
    const { ctx, service } = this;
    const user_id = ctx.curAdminUser && ctx.curAdminUser.id || '';
    if (!user_id) {
      ctx.response.fail(403);
      return;
    }
    const code = ctx.request.body.code;
    if(code == '130401'){
       ctx.response.failByCode('0001');
       return;
    }
    const allAreas =await service.admin.sysArea.queryAllArea();
    const paretAreas = allAreas.length > 0 && allAreas.map(obj=>obj.parent_code) || [];
    if(paretAreas.includes(code)){
       ctx.response.failByCode('0001');
       return;
    }
    const result =  await service.admin.sysBranch.findBranchByCodeOfPetRegister(code);
    if(result.length > 0){
      ctx.response.failByCode('0001');
      return;
    }
    await service.admin.sysArea.delArea(code);
    ctx.response.success();
    const des = `【${ctx.curAdminUser.realName}】在【${ctx.getDate_YYYY_MM_DD_HH_mm_ss()}】删除的机构代码为【${code}】`;
    ctx.writeSysOperationLogs('删除区域', ctx.getSystemLogType('DELETE'), { curAdminUser: ctx.curAdminUser, des });
  }
  /**
  * 更新区域
  */
  async updateArea() {
    const { ctx, service } = this;
    const user_id = ctx.curAdminUser && ctx.curAdminUser.id || '';
    if (!user_id) {
      ctx.response.fail(403);
      return;
    }
    const areaParams = ctx.request.body;
    const areaModel = {};
    if (!areaParams.code) {
      ctx.response.fail(400);
      return;
    }
    if (areaParams.name) {
      areaModel.name = areaParams.name;
    }
    if (areaParams.remarks) {
      areaModel.remarks = areaParams.remarks;
    }
    if (areaParams.longitude) {
      areaModel.longitude = areaParams.longitude;
    }
    if (areaParams.latitude) {
      areaModel.latitude = areaParams.latitude;
    }
    areaModel.update_time = new Date();
    await service.admin.sysArea.updateArea(areaParams.code, areaModel);
    ctx.response.success();
    // 添加操作日志
    const des = `【${ctx.curAdminUser.realName}】在【${ctx.getDate_YYYY_MM_DD_HH_mm_ss()}】更改的区域代码为【${JSON.stringify(areaModel)}】`;
    ctx.writeSysOperationLogs('更新区域', ctx.getSystemLogType('UPDATE'), { curAdminUser: ctx.curAdminUser, des });
  }
  /**
  * 根据code查询区域
  */
  async findAreaByCode() {
    const { ctx, service } = this;
    const areaParams = ctx.request.body;
    if (!areaParams.code) {
      ctx.response.fail(400);
      return;
    }
    const result = await service.admin.sysArea.findAreaByCode(areaParams.code);
    ctx.response.success(result);
    // 添加操作日志
    const des = `【${ctx.curAdminUser.realName}】在【${ctx.getDate_YYYY_MM_DD_HH_mm_ss()}】查询的区域代码为【${areaParams.code}】`;
    ctx.writeSysOperationLogs('根据code查询区域', ctx.getSystemLogType('QUERY'), { curAdminUser: ctx.curAdminUser, des });
  }
}

module.exports = SysAreaController;
